<template>
  <div class="charge-dialog" v-if="visible">
    <div class="charge-card-box">
      <div class="charge-header">
        <span class="charge-title">收费</span>
        <span class="charge-close" @click="onClose">×</span>
      </div>
      <div class="charge-card">
        <div class="charge-amount-row">
          <span style="color: #18b3ab; margin-top: 10px">￥</span
          ><span class="charge-amount">{{ (Number(amount) / 100).toFixed(2)  }}</span>
        </div>
        <div class="charge-pay-methods-grid">
          <div
            v-for="(item, idx) in payMethods"
            :key="item.value"
            :class="['pay-method-grid', { active: idx === selectedMethod }]"
            @click="selectMethod(idx)"
          >
            <div style="display: flex; align-items: center">
              <span class="pay-icon" v-if="item.icon">
                <img :src="item.icon" alt="" />
              </span>
              <span class="pay-label">{{ item.label }}</span>
              <span v-if="item.balance" class="pay-balance"
                >余额:￥{{ (item.balance/100).toFixed(2) }}</span
              >
            </div>
            <span class="pay-radio">
              <span
                :class="['radio-dot', { checked: idx === selectedMethod }]"
              />
            </span>
          </div>
        </div>
        <div class="charge-detail-row">
          <span class="charge-detail-label">{{
            payMethods[selectedMethod]?.label
          }}</span>
          <span class="charge-detail-amount">￥{{ (Number(amount) / 100).toFixed(2) }}</span>
        </div>
        <div class="charge-info-row">
          <span>收费员：{{ cashier }}</span>
          <span>收费时间：{{ chargeTime }}</span>
        </div>
        <div class="charge-footer">
          <label class="charge-print">
            <input type="checkbox" v-model="printRegister" /> 同步打印挂号单
          </label>
          <button class="charge-confirm" @click="onConfirm">完成收费</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, defineProps, defineEmits } from 'vue';
import stores from '@/store';
const useBaseData = stores.useBaseDataStore();

//支付方式
// const payChannelEnum = useBaseData.getEnumSelect('payChannelEnum');
import {getMemberBalance} from '@/api/fee';
const props = defineProps({
  visible: Boolean,
  patientId: { type: [String, Number], default: '' },
  amount: { type: [String, Number], default: '0.00' },
  cashier: { type: String, default: '张三三' },
  chargeTime: { type: String, default: '2025-01-01 12:12' },
});
const emits = defineEmits(['close', 'confirm']);

import iconCard from '@/assets/pay/icon_card_nor.svg';
import iconMoney from '@/assets/pay/icon_money_nor.svg';
import iconWechat from '@/assets/pay/icon_wechat_nor.svg';
import iconZhifubao from '@/assets/pay/icon_zhifubao_nor.svg';
// {"payChannel":"4001","itemIdList":[54,1,2,2,3,4,3],"orderId":46}
const payMethods = ref<any[]>([  
  { label: '会员卡', value: '6001', icon: iconCard, balance: '' },
  { label: '支付宝', value: '4001', icon: iconZhifubao, balance: '' },
  { label: '微信', value: '3001', icon: iconWechat, balance: '' },
  { label: '现金', value: '5001', icon: iconMoney, balance: '' },
])

const patientId = ref(props.patientId)
const getMemberBalanceApi = async () => {
  //console.log(patientId.value);
  let res = await getMemberBalance(patientId.value)
  if(res.code == '200'){
    for (let index = 0; index < payMethods.value.length; index++) {
      let item = payMethods.value[index];
      if(item.value == '6001'){
        item.balance = res.data.balance.toFixed(2)
      }
    }
  }else{
 
    
  }
}
getMemberBalanceApi()


const selectedMethod = ref(0);
const printRegister = ref(false);

function selectMethod(idx: number) {
  
  selectedMethod.value = idx;
}
function onClose() {
  emits('close');
}
function onConfirm() {
  let data = {
    payChannel: payMethods.value[selectedMethod.value]?.value ?? '',
  }

  emits('confirm', {
    data: data,
    printRegister: printRegister.value,
  });
}
</script>

<style scoped>
.charge-dialog {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.charge-card {
  padding: 0px 14px 18px 14px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}
.charge-card-box {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  background: #fff;
  border-radius: 16px;
  width: 450px;
  box-shadow: 0 4px 24px 0 rgba(0, 0, 0, 0.18);
  overflow: hidden;
}
.charge-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 16px;
  font-weight: 600;
  padding: 10px 24px;

  color: #222;
  background: linear-gradient(90deg, #e4f9f7 0%, #fffff4 100%);
}
.charge-title {
  font-size: 20px;
  font-weight: 600;
  color: #222;
}
.charge-close {
  font-size: 16px;
  color: #999;
  cursor: pointer;
  font-weight: 400;
  transition: color 0.2s;
}
.charge-close:hover {
  color: #18b3ab;
}
.charge-amount-row {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 18px;
  margin-top: 18px;
  height: 78px;
  background: rgba(231, 231, 231, 0.2);
  border-radius: 6px 6px 6px 6px;
  border: 1px solid #ffffff;
}
.charge-amount {
  font-size: 28px;
  color: #18b3ab;
  font-weight: 700;
}
/* 四格支付方式区域 */
.charge-pay-methods-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 12px;
  margin-bottom: 18px;
}
.pay-method-grid {
  display: flex;
  align-items: center;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  background: #fafbfc;
  justify-content: space-between;
  min-height: 42px;
  position: relative;
  transition: border-color 0.2s, background 0.2s;
}
.pay-method-grid.active {
  border-color: #18b3ab;
  background: #e6f7f5;
}
.pay-method-grid .pay-icon {
  margin-right: 3px;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pay-method-grid .pay-label {
  font-size: 13px;
  color: #222;
  font-weight: 500;
}
.pay-method-grid .pay-balance {
  font-size: 11px;
  color: rgba(0, 0, 0, 0.4);
  margin-left: 8px;
}
.pay-method-grid .pay-radio {
  margin-left: 10px;
  display: flex;
  align-items: center;
}
.pay-method-grid .radio-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #e0e0e0;
  background: #fff;
  display: inline-block;
  position: relative;
  transition: border-color 0.2s;
}
.pay-method-grid.active .radio-dot {
  border-color: #18b3ab;
}
.pay-method-grid.active .radio-dot::after {
  content: '';
  display: block;
  width: 10px;
  height: 10px;
  background: #18b3ab;
  border-radius: 50%;
  position: absolute;
  left: 2px;
  top: 2px;
}
.pay-icon {
  margin-right: 3px;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pay-label {
  font-size: 16px;
  color: #222;
  font-weight: 500;
}
.pay-balance {
  font-size: 13px;
  color: #18b3ab;
  margin-left: 8px;
}
.pay-radio {
  margin-left: auto;
  display: flex;
  align-items: center;
}
.radio-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid #e0e0e0;
  background: #fff;
  display: inline-block;
  position: relative;
  transition: border-color 0.2s;
}
.radio-dot.checked {
  border-color: #18b3ab;
}
.radio-dot.checked::after {
  content: '';
  display: block;
  width: 10px;
  height: 10px;
  background: #18b3ab;
  border-radius: 50%;
  position: absolute;
  left: 3px;
  top: 3px;
}
.charge-detail-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-radius: 6px;
  padding: 10px 14px;
  font-size: 16px;
  color: #222;
  margin-bottom: 12px;

  background: #fdfdfd;
  border-radius: 6px 6px 6px 6px;
  border: 1px solid #e7e7e7;
}
.charge-detail-label {
  font-size: 13px;
  color: rgba(0, 0, 0, 0.9);
}
.charge-detail-amount {
  font-weight: 600;
  font-size: 16px;
}
.charge-info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  color: #999;
  margin-bottom: 12px;
}
.charge-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}
.charge-print {
  font-size: 15px;
  color: #666;
  display: flex;
  align-items: center;
  gap: 4px;
}
.charge-confirm {
  background: #18b3ab;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
  padding: 6px 20px;
}
.charge-confirm:hover {
  background: #139999;
}
</style>
